package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.RealName;
import com.baomidou.ant.service.IRealNameService;
import com.baomidou.ant.utils.CheckNameUtils;
import com.baomidou.ant.utils.IdCardUtils;
import com.baomidou.ant.utils.MailUtils;
import com.baomidou.ant.utils.VoUtilsTool;
import com.baomidou.ant.vo.Request.RequestMyRealNameVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.google.code.kaptcha.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/real-name")
public class RealNameController {
    @Autowired
    private IRealNameService realNameService;

    //邮箱验证码接口----string类型
    @GetMapping("/sendEmail/{Email}")
    public Result sendEmail(@PathVariable("Email") String email, HttpServletRequest request){
        //生成 6 位验证码
        Random random=new Random();
        int max=999999;
        int min=100000;
        int code = random.nextInt(max);//code是int类型
        if(code<min){
            code=code+min;
        }
        //System.out.println(mobile+"收到验证码是："+code);
        //发送实现
        try {
            MailUtils.sendMail(email, String.valueOf(code));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //缓存验证码code,存入session中
        HttpSession session = request.getSession();
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY,code);
        //设置过期时间120s
        session.setMaxInactiveInterval(120);
        return Result.getSuccess().setMsg("成功发送验证码，请注意查收邮箱");
    }

    //实名认证接口
    @GetMapping("/toReal/{userId}")
    public Result toReal(@PathVariable Integer userId){
        //判断是否实名过，如果实名过了，则无法提交实名
        QueryWrapper<RealName>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        RealName realName=realNameService.getOne(queryWrapper);
        if(realName!=null){
            return Result.getFailure();
        }else
            return Result.getSuccess();
    }

    //提交实名认证
    @PostMapping("/realName")
    public Result realName(@RequestBody RequestMyRealNameVo vo,HttpServletRequest request){
        //信息为空
//        if (VoUtilsTool.checkObjFieldIsNull(vo)){
//            return Result.getFailure().setMsg("验证码!");
//        }
        //身份证号码判断
        if(!IdCardUtils.isIdCard(vo.getIdCard())){
            return Result.getFailure().setMsg("请输入正确的身份证号");
        }
        //姓名判断
        if(!CheckNameUtils.checkNameValid(vo.getUserName())){
            return Result.getFailure().setMsg("请输入正确的姓名,目前只支持中文姓名");
        }
        //判断邮箱验证码
        HttpSession session = request.getSession();
        if(ObjectUtils.isEmpty(session.getAttribute(Constants.KAPTCHA_SESSION_KEY))){
            return Result.getFailure().setMsg("请先获取验证码");
        }
        String trueCode = session.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString();
        if (!trueCode.equals(vo.getCode())) {
            return Result.getFailure().setMsg("邮箱验证码错误");
        }
        //提交实名信息
        if(realNameService.putMyRealName(vo)){
            return Result.getSuccess().setMsg("实名信息提交成功，请谨慎充值");
        }else
            return Result.getFailure().setMsg("实名失败,该身份证已经被实名");
    }

}
